package com.OA.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.OA.entity.User;
import com.OA.service.LoginService;
import com.alibaba.fastjson.JSON;

/**
 * 登录注册
 * @author thinkpad
 *
 */
@Controller
@RequestMapping("login")
public class LoginController {

	@Autowired
	private LoginService loginService;
	
	@Autowired
	public static Map<String, Object> map = new HashMap<String, Object>();
	 
	/**
	 * 用户登录
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	@RequestMapping(value="loadUserByName.do")
	public void loadUserByName(HttpServletRequest req,HttpServletResponse resp) throws IOException{
		resp.setCharacterEncoding("utf-8");
		String userName = req.getParameter("userName");
		String userPass = req.getParameter("userPass");
		System.out.println(userName+"---"+userPass);
		String msg = null;
		if(userName !=""){
			User user = loginService.loadUserByName(userName);
			if(userName.equals(user.getUserName())){
				if(userPass.equals(user.getUserPass())){
					System.out.println("登录成功");
					req.getSession().setAttribute("user", user);
					resp.getWriter().write("{\"msg\":\"ok\"}");
				}else{
					resp.getWriter().write("{\"msg\":\"密码错误\"}");
				}
			}else{
				resp.getWriter().write("{\"msg\":\"用户名错误\"}");
			}
		}else{
			resp.getWriter().write("{\"msg\":\"用户名不能为空\"}");
		}
		resp.getWriter().flush();
		resp.getWriter().close();
	}
}
